package com.nf.health.app.adapter;

/**
 * 万能Adapter，查看KJAdapter
 * 
 * @author kymjs(http://www.kymjs.com/)
 */
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterHolder {
	private final SparseArray<View> mViews;
	private final int mPosition;
	private final View mConvertView;

	private AdapterHolder(ViewGroup parent, int layoutId, int position) {
		this.mPosition = position;
		this.mViews = new SparseArray<View>();
		mConvertView = LayoutInflater.from(parent.getContext()).inflate(
				layoutId, parent, false);
		// setTag
		mConvertView.setTag(this);
	}

	/**
	 * 拿到全部View
	 * 
	 * @return
	 */
	public SparseArray<View> getAllView() {
		return mViews;
	}

	/**
	 * 拿到一个ViewHolder对象
	 * 
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	public static AdapterHolder get(View convertView, ViewGroup parent,
			int layoutId, int position) {
		if (convertView == null) {
			return new AdapterHolder(parent, layoutId, position);
		} else {
			return (AdapterHolder) convertView.getTag();
		}
	}

	public View getConvertView() {
		return mConvertView;
	}

	/**
	 * 通过控件的Id获取对于的控件，如果没有则加入views
	 * 
	 * @param viewId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends View> T getView(int viewId) {
		View view = mViews.get(viewId);
		if (view == null) {
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}

	/**
	 * 为TextView设置字符串
	 * 
	 * @param viewId
	 * @param text
	 * @return
	 */
	public AdapterHolder setText(int viewId, String text) {
		TextView view = getView(viewId);
		view.setText(text);
		return this;
	}

	/**
	 * 为ImageView设置图片
	 * 
	 * @param viewId
	 * @param drawableId
	 * @return
	 */
	public AdapterHolder setImageResource(int viewId, int drawableId) {
		ImageView view = getView(viewId);
		view.setImageResource(drawableId);

		return this;
	}

	/**
	 * 为ImageView设置图片
	 * 
	 * @param viewId
	 * @param bm
	 * @return
	 */
	public AdapterHolder setImageBitmap(int viewId, String url) {
		ImageView iv = getView(viewId);
		ImageLoader.getInstance().displayImage(url, iv);
		return this;
	}
	/**
	 * 为ImageView设置图片
	 *
	 * @param viewId
	 *  解决ImageLoader的闪烁问题
	 * @return
	 */
	public AdapterHolder setImageBitmap2(int viewId,String url, final int defaultImageResouce) {
		final ImageView iv = getView(viewId);
		ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				iv.setImageResource(defaultImageResouce);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				iv.setImageResource(defaultImageResouce);
			}

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(loadedImage);
			}

			@Override
			public void onLoadingCancelled(String imageUri, View view) {

			}
		});
		return this;
	}

	
	
	public int getPosition() {
		return mPosition;
	}
}